﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
using System.Web.Security;
using Beway.ValueAdded.Model;
using Beway.ValueAdded.Service;

namespace Beway.ValueAdded.Admin.Controllers
{
    public class LoginController : BaseController
    {
        /// <summary>
        /// 服务类
        /// </summary>
        public AdminUserServ service = new AdminUserServ();

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Login(string username, string pwd, string valcode)
        {
            var res = new JsonResult();
            if (valcode.Trim() == this.GetValidCodeInSession())
            {
                if (service.Login(username, pwd))
                {
                    AdminUser manager = service.GetByUserName(username);
                    //if (manager.RoleID == 0)
                    //{
                    if (!manager.LoginDisabled)
                    {
                        Session["role"] = manager.RoleID;
                        FormsAuthentication.SetAuthCookie(username, false);
                        res.Data = "OK";
                    }
                    else
                    {
                        res.Data = "抱歉，您的账户被禁止登录。";
                    }
                    //}
                    //else
                    //{
                    //    res.Data = "商户管理员不能登录此系统。";
                    //}
                }
                else
                {
                    res.Data = "用户名或密码不正确。";
                }
            }
            else
            {
                res.Data = "验证码不正确，请重新输入。";
            }
            return res;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldpass"></param>
        /// <param name="newpass"></param>
        /// <returns></returns>
        public JsonResult EditPass(string oldpass, string newpass)
        {
            JsonResult res = new JsonResult();
            string UserName = User.Identity.Name;
            bool isedit = service.EditPass(UserName, oldpass, newpass);
            if (isedit)
            {
                res.Data = "ok";
            }
            else
            {
                res.Data = "原密码不正确，请重试";
            }
            return res;
        }

        public RedirectResult Logout()
        {
            FormsAuthentication.SignOut();
            RedirectResult rr = new RedirectResult("/login/login");
            return rr;

        }

    }
}
